package com.hitqz.robot.biz.flow.component.agv;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hitqz.robot.api.business.enums.RobotOtherCommandEnum;
import com.hitqz.robot.api.business.model.dto.FlowContextDto;
import com.hitqz.robot.api.business.model.dto.FlowPatrolPreAndEndTicketsDto;
import com.hitqz.robot.api.common.entity.Robot;
import com.hitqz.robot.api.common.entity.RobotMapPos;
import com.hitqz.robot.api.common.entity.TicketsTask;
import com.hitqz.robot.biz.business.dispatch.DispatchService;
import com.hitqz.robot.biz.service.RobotMapPosService;
import com.hitqz.robot.biz.service.RobotService;
import com.hitqz.robot.common.core.util.R;
import com.hitqz.robot.driver.model.IRobotPosDto;
import com.hitqz.robot.driver.parent.RobotDriverService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @author xupkun
 * @date 2024/12/3
 */

@Slf4j
@LiteflowComponent(value = "GoTargetAndWaitForLifter", name = "[轮对/轮轴]前往取轮对")
@AllArgsConstructor
public class GoTargetAndWaitForLifter extends NodeComponent {

    private final RobotDriverService robotDriverService;

    private final RobotMapPosService robotMapPosService;

    private final RobotService robotService;

    private final DispatchService dispatchService;


    @Override
    public void process() throws Exception {
        FlowContextDto dto =  this.getRequestData();
        TicketsTask task = dto.getTicketsTask();
        log.info("任务数据 {}", task.getParseOrderParams());
        Robot robot = robotService.getDefaultRobot();
        FlowPatrolPreAndEndTicketsDto ticketsDto = JSONObject.parseObject(dto.getTicketsTask()
                .getParseOrderParams(), FlowPatrolPreAndEndTicketsDto.class);
        String sourceName = ticketsDto.getModalData().get("sourcePosName")== null ?
                String.valueOf(ticketsDto.getModalData().get("sourcePosCode")):
                String.valueOf(ticketsDto.getModalData().get("sourcePosName"));
        log.info("sourceName {}", sourceName);
        RobotMapPos robotMapPos = robotMapPosService.getOne(Wrappers.<RobotMapPos>lambdaQuery()
                .eq(RobotMapPos::getName, sourceName)
                .eq(RobotMapPos::getMapCode, robot.getMapCode()));
        if (robotMapPos == null) {
            robotMapPos = robotMapPosService.getOne(Wrappers.<RobotMapPos>lambdaQuery()
                    .eq(RobotMapPos::getCode, robotMapPos.getCode())
                    .eq(RobotMapPos::getMapCode, robot.getMapCode()));
        }
        log.info("前往终端目标点{}", robotMapPos.getCode());
        boolean isSuccess = dispatchService.go2TargetPosition(robotMapPos, false);
        TimeUnit.SECONDS.sleep(3);
        while (true) {
            IRobotPosDto iRobotPosDto = robotDriverService.getWebPos();
            boolean isReach = false;
            if (iRobotPosDto !=null && iRobotPosDto.isReached()) {
                isReach = true;
            }
            R result = robotDriverService.executeOtherCommand(RobotOtherCommandEnum.JACKING_STATUS,null);
            int jackStatus = (int) result.getData();
            log.info("机器人顶升状态" + jackStatus);
            if (isReach && jackStatus != 0x01) {
                log.info("机器人到达了但是没提示顶生,应该提示错误暂时不处理");
                break;
            }
            if (0x01 == jackStatus) {
                log.info("机器人顶升完成");
                break;
            }
            TimeUnit.SECONDS.sleep(3);
        }
    }
}
